const app = getApp()
//引入请求接口文件
import Serv from './findServer';
var regeneratorRuntime = require("../../lib/js/runtime-module.js")
import Config from '../../utils/config2'
Page({
  data: {
    cityCode: '',
    cityName: '',
    params: {},
    name: '',
    scrollTop:0
  },
  onLoad(){
  
  },
  //切换地址
  changeAdd() {
   
    const that = this;
    wx.navigateTo({
      url: "/pages/switchCity/switchCity",
      success: function(){
        app.globalData.findHouseData = {}
        that.setData({
          name: '',
          params: {},
        })
      }
    })
  },
  toBuildingDetail: function (e) {
    let index = e.currentTarget.dataset.index;
    const that = this;
    let {isLogin } = this.data
    wx.navigateTo({
      url: isLogin ? `/subPackages/pages/buildingDetail/buildingDetail?productId=${this.data.listData[index].productId}` :'/pages/biz/login/login',
      success: function(){
        app.globalData.findHouseData = that.data.params;
      }
    })
  },

  onReady: function () {
  },
  //页面上拉
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },

  // 子组件回调
  filerFun: function (clildrenObj) {
    console.log(clildrenObj)
    let params = clildrenObj.detail // 获取子组件回调筛选的对象
    this.setData({
      params: params
    })
    params.pageNo = 1
    params.pageSize = 10
    let key = app.globalData.searchKey // 搜索关键字
    if (key != '') {
      params.name = key
    }
    params.city = this.data.cityCode;
    // app.globalData.findHouseData = params;
    this.getHouseList(params, 0)
  },

  //找房接口
  async getHouseList(parmas, isPush) {
    wx.showLoading({
      title: '加载中',
    })
    console.log('找房数据参数-->', parmas)
    let result = await Serv.getfindList(parmas)
    if (result.success) {
      let listData = result.rows;
      if (isPush == 0) {
        this.setData({
          listData: listData,
          notBeginData: result
        })
        console.log(this.data.listData)
      } else {
        let notBeginData = this.data.notBeginData // 获取原本保存的数据
        notBeginData.pageNo = result.pageNo // 新的当前页
        let rows = result.rows // 数据列表
        let listData = this.data.listData
        let oldrows = listData // 原本的数据列表
        let newList = oldrows.concat(rows) // 合并
        listData = newList // 新列表合并后赋值
        this.setData({
          listData: listData,
          notBeginData: notBeginData
        })
      }
      wx.hideLoading();
    }
  },
  onShow() {
    this.setData({
      // isLogin: wx.getStorageSync(Config.userIdKey).length > 0 ? true : false
      isLogin:true
    })
    this.filter = this.selectComponent("#selectId");
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 1];//上一页面
  
    
    const findHouseData = app.globalData.findHouseData;
    let {
      city = '',
      cityadcode = '',
    } = app.globalData.cityData || {};
    const name = app.globalData.searchKey || ""
    if (city.indexOf('市') > -1) {
      city = city.slice(0, -1);
    }
    
    const params = {
      ...this.data.params,
      ...findHouseData,
      city: cityadcode,
      name
    }
    if (prevPage.__displayReporter.showReferpagepath != "subPackages/pages/buildingDetail/buildingDetail.html" && cityadcode != this.data.cityCode){
      this.getHouseList(params, 0)
    }
    if(prevPage.__displayReporter.showReferpagepath == "subPackages/pages/search/search.html"){
      this.getHouseList(params,0);
    }
    this.setData({
      cityName: city,
      cityCode: cityadcode,
      name,
    })
  },
  gotoMap() {
    wx.showToast({
      title: '敬请期待',
      icon: 'none'
    })
  },
  onReachBottom: function () {
    let total = Number.parseInt(this.data.notBeginData.total) // 总条数
    let totalPage = 1 // 总页数
    if (total % 10 === 0) {
      totalPage = Number.parseInt(total / 10)
    } else {
      totalPage = Number.parseInt(total / 10) + 1
    }
    let pageNo = Number.parseInt(this.data.notBeginData.pageNo) // 当前页数
    if (pageNo < totalPage && (pageNo + 1) <= totalPage) { // 如果当前条数小于总条数
      let pageParams = {
        ...this.data.params,
        city: app.globalData.cityData?app.globalData.cityData.cityadcode:null,
        pageNo: pageNo + 1,
        pageSize: 10,
        name:this.data.name
      }
      this.getHouseList(pageParams, 1)
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
